﻿using AutoMapper;
using Enferno.Public.Web.Models;
using Enferno.StormApiClient.Shopping;

namespace Enferno.Public.Web.Builders
{
    public class CheckoutBuilder :
        CheckoutBaseBuilder
            <CheckoutPrivatePayerModel, CheckoutPrivateShipToModel, CheckoutCompanyPayerModel, CheckoutCompanyShipToModel, PrivateCheckoutModel, CompanyCheckoutModel, BasketModel, BasketItemModel, PromotionModel, OnHandStatusModel,
                PriceModel, ProductFileModel, CheckoutBuyerModel, DeliveryMethodModel, PaymentMethodModel,
                PaymentServiceModel, PersonInformationModel, CompanyInformationModel, AddressModel>
    {
        
    }

    public class CheckoutBaseBuilder<
        TCheckoutPrivatePayerModel,
        TCheckoutPrivateShipToModel,
        TCheckoutCompanyPayerModel,
        TCheckoutCompanyShipToModel,
        TPrivateCheckoutModel,
        TCompanyCheckoutModel,
        TBasketModel,
        TBasketItemModel,
        TPromotionModel,
        TOnHandStatusModel,
        TPriceModel,
        TProductFileModel,
        TCheckoutBuyerModel,
        TDeliveryMethodModel,
        TPaymentMethodModel,
        TPaymentServiceModel,
        TPersonInformationModel,
        TCompanyInformationModel,
        TAddressModel> : BuilderBase
        where TCheckoutPrivatePayerModel: CheckoutPrivatePayerBaseModel<TPersonInformationModel, TAddressModel>
        where TCheckoutPrivateShipToModel : CheckoutPrivateShipToBaseModel<TPersonInformationModel, TAddressModel>
        where TCheckoutCompanyPayerModel: CheckoutCompanyPayerBaseModel<TCompanyInformationModel, TAddressModel>
        where TCheckoutCompanyShipToModel: CheckoutCompanyShipToBaseModel<TCompanyInformationModel, TAddressModel>
        where TPrivateCheckoutModel: PrivateCheckoutBaseModel<TCheckoutPrivatePayerModel, TCheckoutPrivateShipToModel, TCheckoutBuyerModel, TBasketModel, TBasketItemModel, TPromotionModel, TOnHandStatusModel, TPriceModel, TProductFileModel, TDeliveryMethodModel, TPaymentMethodModel, TPaymentServiceModel, TPersonInformationModel, TAddressModel>
        where TCompanyCheckoutModel : CompanyCheckoutBaseModel<TCheckoutCompanyPayerModel, TCheckoutCompanyShipToModel, TCheckoutBuyerModel, TBasketModel, TBasketItemModel, TPromotionModel, TOnHandStatusModel, TPriceModel, TProductFileModel, TDeliveryMethodModel, TPaymentMethodModel, TPaymentServiceModel, TCompanyInformationModel, TAddressModel> 
        where TBasketModel : BasketBaseModel<TBasketItemModel, TPromotionModel, TOnHandStatusModel, TPriceModel, TProductFileModel>
        where TBasketItemModel : BasketItemBaseModel<TPriceModel>
        where TPromotionModel : PromotionBaseModel<TProductFileModel>
        where TOnHandStatusModel : OnHandStatusModel
        where TPriceModel : PriceModel
        where TProductFileModel : ProductFileModel
        where TCheckoutBuyerModel : CheckoutBuyerModel
        where TDeliveryMethodModel : DeliveryMethodModel
        where TPaymentMethodModel : PaymentMethodBaseModel<TPaymentServiceModel>
        where TPaymentServiceModel : PaymentServiceModel
        where TPersonInformationModel: PersonInformationModel
        where TCompanyInformationModel: CompanyInformationModel
        where TAddressModel: AddressModel
    {
        public
            CheckoutBaseModelBase
                <TBasketModel, TBasketItemModel, TPromotionModel, TOnHandStatusModel, TPriceModel, TProductFileModel,
                    TCheckoutBuyerModel, TDeliveryMethodModel, TPaymentMethodModel, TPaymentServiceModel, TAddressModel>
            MapCheckoutModel(Checkout checkout)
        {
            var isPrivate = checkout.Payer.Companies == null || checkout.Payer.Companies.Count == 0;

            if (isPrivate)
            {
                return Mapper.Map<Checkout, TPrivateCheckoutModel>(checkout);
            }

            return Mapper.Map<Checkout, TCompanyCheckoutModel>(checkout);
        }


    }
}
